package com.fc.demo.filter;

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @author:Teacher黄
 * @date:Created at 2020/04/23
 */
public class RolesOrFilter extends AuthorizationFilter {
    @Override
    protected boolean isAccessAllowed(
            ServletRequest servletRequest,
            ServletResponse servletResponse,
            Object o) throws Exception {

        // 1. 获取主体
        Subject subject = getSubject(servletRequest, servletResponse);
        // 2. 获取角色数据
        String[] roles = (String[]) o;
        // 判断roles
        if(roles == null || roles.length == 0){
            // 说明不需要任何的角色就可以直接访问
            return true;
        }
        // 遍历
        for (String role : roles) {
            if (subject.hasRole(role)) {
                // 只要拥有其中的一个角色, 就直接允许访问
                return true;
            }
        }
        // 表示没有对应的访问权限
        return false;
    }
}
